package com.lagou.client;

import com.lagou.mmclass.ClientScanner;
import com.lagou.mmclass.User;
import com.lagou.mmclass.UserMessage;
import com.lagou.server.ServerView;

import java.io.IOException;

public class StudentCrud {
    private ClientInit cic;
    private ServerView sb;

//    public StudentCrud(ClientInit cic) {
//        this.cic = cic;
//    }


    public StudentCrud(ClientInit cic, ServerView sb) {
        this.cic = cic;
        this.sb = sb;
    }

    public void addUser() throws IOException {
        System.out.println("请输入你要添加的学生姓名,密码");
        String strUser = ClientScanner.getScanner().next();
        String[] strings = strUser.split(",");
        User user = new User(strings[0], strings[1]);
        UserMessage uum = new UserMessage("addCheck", user);
        cic.getOos().writeObject(uum);


    }
    public void deleteUser() throws IOException {
        System.out.println("请输入你要删除的学生姓名,密码");
        String strUser = ClientScanner.getScanner().next();
        String[] strings = strUser.split(",");
        User user = new User(strings[0], strings[1]);
        for (User ts:sb.getUserList()){
            if(user.equals(ts)){
                sb.getUserList().remove(ts);
                System.out.println("删除成功，被删除的学生信息是：" +ts);
            }
        }

    }
    public void modifyUser() throws IOException {
        System.out.println("请输入你要修改的学生信息");
        String strUser = ClientScanner.getScanner().next();
        String[] strings = strUser.split(",");
        User user = new User(strings[0], strings[1]);
        for (User ts:sb.getUserList()){
            if(user.equals(ts)){
                System.out.println("请输入修改后的信息姓名：");
                String name = ClientScanner.getScanner().next();
                System.out.println("请输入修改后的密码：");
                String password = ClientScanner.getScanner().next();
                User user1 = new User(name, password);
                sb.getUserList().set(0,user1);
            }
        }
//        UserMessage uum = new UserMessage("modifyCheck", user);
//        cic.getOos().writeObject(uum);
    }
    public void queryUser(){
        System.out.println("请输入你要查找的学生姓名");
        String strname = ClientScanner.getScanner().next();
        for(int i=0;i<sb.getUserList().size();i++){
            if(strname==sb.getUserList().get(i).getName()){
                System.out.println("你要找的学生信息"+sb.getUserList().get(i));
            }
        }



    }

}
